﻿<script lang="ts" name="appTeam" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { Plus } from "@element-plus/icons-vue";
import { UploadRequestOptions } from "element-plus";
import { useAppTeamApi } from '/@/api/team/appTeam';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const appTeamApi = useAppTeamApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  name: [{required: true, message: '请选择名称！', trigger: 'blur',},],
  status: [{required: true, message: '请选择状态！', trigger: 'change',},],
});

// 页面加载时
onMounted(async () => {
  const data = await appTeamApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.provinceCode = data.provinceCode ?? [];
  state.dropdownData.districtCode = data.districtCode ?? [];
  state.dropdownData.cityCode = data.cityCode ?? [];
  state.dropdownData.directorId = data.directorId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? { status: 1,orderNo: 100, };
	state.ruleForm = row.id ? await appTeamApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await appTeamApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

const uploadAvatarHandle = async (options: UploadRequestOptions) => {
	const res = await appTeamApi.uploadAvatar(options);
	state.ruleForm.avatar = res.data.result?.url;
	};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="appTeam-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="口号" prop="slogan">
							<el-input v-model="state.ruleForm.slogan" placeholder="请输入口号" maxlength="256" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="队徽" prop="avatar">
							<el-upload
								list-type="picture-card"
								:show-file-list="false"
								:http-request="uploadAvatarHandle">
								<img
									v-if="state.ruleForm.avatar"
									:src="state.ruleForm.avatar"
									@click="state.ruleForm.avatar=''"
									style="width: 100%; height: 100%; object-fit: contain"/>
									<el-icon v-else><Plus /></el-icon>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="省份" prop="provinceCode">
							<el-select clearable filterable v-model="state.ruleForm.provinceCode" placeholder="请选择省份">
								<el-option v-for="(item,index) in state.dropdownData.provinceCode" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="区" prop="districtCode">
							<el-select clearable filterable v-model="state.ruleForm.districtCode" placeholder="请选择区">
								<el-option v-for="(item,index) in state.dropdownData.districtCode" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="市" prop="cityCode">
							<el-select clearable filterable v-model="state.ruleForm.cityCode" placeholder="请选择市">
								<el-option v-for="(item,index) in state.dropdownData.cityCode" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="级别" prop="level">
							<el-input-number v-model="state.ruleForm.level" placeholder="请输入级别" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="类型" prop="type">
							<el-input v-model="state.ruleForm.type" placeholder="请输入类型" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="负责人Id" prop="directorId">
							<el-select clearable filterable v-model="state.ruleForm.directorId" placeholder="请选择负责人Id">
								<el-option v-for="(item,index) in state.dropdownData.directorId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.id" >
						<el-form-item label="状态" prop="status">
							<g-sys-dict v-model="state.ruleForm.status" code="StatusEnum" render-as="select" placeholder="请选择状态" clearable filterable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="128" show-word-limit clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>